package com.app.business.web.business;

import com.app.business.model.OverAreaPrice;
import com.app.business.model.OverAreaPriceNew;
import com.app.business.model.Parameter;
import com.app.business.model.ZsProject;
import com.app.business.service.*;
import com.app.system.jpa.PageBean;
import com.app.system.permission.model.DataDict;
import com.app.system.permission.service.DataDictService;
import com.app.system.utils.CollectionUtils;
import com.app.system.utils.ParamUtils;
import com.app.system.utils.WebUtils;
import com.app.system.utils.dataType.DateUtils;
import com.app.system.utils.dataType.StringUtils;
import com.app.system.utils.exception.AjaxException;
import com.app.system.utils.exception.Precondition;
import com.app.system.utils.exception.Response;
import com.app.system.utils.page.LayUiUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 征收项目管理
 * Created by yby on 2019/2/11 14:49.
 */

@Controller
@RequestMapping("/business/parameter/*")
@SuppressWarnings("all")
public class ParameterController {

    @Autowired
    private ZsProjectService zsProjectService;
    @Autowired
    private ParameterService parameterService;
    @Autowired
    private DataDictService dataDictService;
    @Autowired
    private ContractService contractService;
    @Autowired
    private OverAreaPriceNewService overAreaPriceNewService;

    /**
     * 征收项目数据列表
     */
    @RequestMapping("list")
    @RequiresPermissions("parameter:list")
    public void list() {
    }

    /**
     * 加载征收项目数据
     *
     * @return
     */
    @RequestMapping("loadParameterList")
    @ResponseBody
    @RequiresPermissions("parameter:loadParameterList")
    public Map loadFamilyList() {
        List<Map> listData = Lists.newArrayList();
        PageBean<Parameter> parameterPageBean = this.parameterService.getParameterPageBean();
        List<Parameter> parameterList = parameterPageBean.getDatas();
        if (!CollectionUtils.isEmpty(parameterList)) {
            Map<String, Object> item = null;
            for (Parameter parameter : parameterList) {
                item = Maps.newHashMap();
                item.put("id", parameter.getId());
                item.put("name", StringUtils.getNotNull(parameter.getName()));
                item.put("memo", StringUtils.getNotNull(parameter.getMemo()));
                listData.add(item);
            }
        }
        return LayUiUtils.page(parameterPageBean.getTotalCount().intValue(), listData);
    }

    /**
     * 征收项目数据配置表单
     *
     * @param request
     * @param modelMap
     */
    @RequestMapping("dataForm")
    @RequiresPermissions("parameter:dataForm")
    public void dataForm(HttpServletRequest request, ModelMap modelMap) {
        int parameterId = ParamUtils.getInt(request, "parameterId", 0);
        Parameter parameter = this.parameterService.findOne(parameterId);
        if (parameter == null) {
            parameter = new Parameter();
        }
        Precondition.checkSqdsArguement(parameter != null, "未找到相关参数！");
        List<OverAreaPriceNew> overAreaPriceNewList = this.overAreaPriceNewService.getOverAreaPriceNewListByParameterId(parameter.getId());
        modelMap.addAttribute("parameter", parameter);
        modelMap.addAttribute("overAreaPriceNewList", overAreaPriceNewList);
    }

    /**
     * 参数配置数据保存
     *
     * @param request
     * @return
     * @throws AjaxException
     */
    @RequestMapping("dataFormSave")
    @ResponseBody
    @RequiresPermissions("parameter:dataFormSave")
    public synchronized Response dataFormSave(HttpServletRequest request) throws AjaxException {
        Response response = this.parameterService.dataFormSave(request);
        return response;
    }



    /**
     * 删除参数配置
     *
     * @param request
     * @return
     */
    @RequestMapping("delParameterById")
    @ResponseBody
    @RequiresPermissions("parameter:delParameterById")
    public synchronized Response delParameterById(HttpServletRequest request) {
        int parameterId = ParamUtils.getInt(request, "parameterId", 0);
        Parameter parameter = this.parameterService.findOne(parameterId);
        Precondition.checkAjaxArguement(parameter != null, "1111", "数据错误！");
        try {
            //先删除参数配置面积区间信息
            this.overAreaPriceNewService.deleteOverAreaNewPriceByParameterId(parameter.getId());
            this.parameterService.delete(parameter.getId());
        } catch (Exception e) {
            Precondition.checkAjaxArguement(false, "9999", "该参数配置已使用，无法删除");
        }
        return new Response("0001", "操作成功");
    }
}
